package com.dc.business.datacollectbusiness.controller;

import com.dc.business.datacollectbusiness.model.HJ212Model;
import com.dc.business.datacollectbusiness.model.Result;
import com.dc.business.datacollectbusiness.model.device;
import com.dc.business.datacollectbusiness.service.HJ212ReceiverService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/api/testHJ212")
@Slf4j
public class HJ212DataController {

    @Autowired
    private HJ212ReceiverService hj212ReceiverService;

    @GetMapping("/fetchAllDevicesLatestInfo/")
    public Result fetchAllDevicesLatestInfo() {
        List<device> allHJ212MsgLast = hj212ReceiverService.fetchAllDevicesLatestInfo();
        return Result.success(allHJ212MsgLast);
    }

    @GetMapping("/findPageDeviceByMn/")
    public Result findPageDeviceByMn(@RequestParam(value = "pageNum", required = true) int pageNum,
                                     @RequestParam(value = "pageSize", required = true) int pageSize,
                                     @RequestParam(value = "mn", required = true) String mn,
                                     @RequestParam(value = "startTime", required = false) String startTime,
                                     @RequestParam(value = "endTime", required = false) String endTime) {

        if (startTime == null && endTime == null) {
            List<HJ212Model> list = hj212ReceiverService.findPageDeviceByMn(pageNum, pageSize, mn).getList();
            int total = (int)hj212ReceiverService.findPageDeviceByMn(pageNum, pageSize, mn).getTotal();
            return Result.successfy(list, total);
        } else if (startTime != null && endTime != null) {
            List<HJ212Model> list = hj212ReceiverService.findPageDeviceByMnAndTime(pageNum, pageSize, mn, startTime, endTime).getList();
            int total = (int)hj212ReceiverService.findPageDeviceByMnAndTime(pageNum, pageSize, mn, startTime, endTime).getTotal();
            return Result.successfy(list, total);
        }
        return null;
    }
}
